//
//  ByMoneyChangeDetailVC.m
//  caipiao_ios
//
//  Created by mc on 2018/7/10.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByMoneyChangeDetailVC.h"
#import "ZZBPopView.h"
#import "ByWithdrawListCell.h"

@interface ByMoneyChangeDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ByMoneyChangeDetailVC

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    [self initViews];
    
   
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
   
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
    [titleView addSubview:titleLabel];
    titleLabel.text = @"账户明细";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentRight;
    self.titleLabel = titleLabel;
    if (self.type == 0) {
        self.titleLabel.text = @"账户明细";
    }else if (self.type == 1){
        self.titleLabel.text = @"充值明细";
    }else if (self.type == 2){
        self.titleLabel.text = @"提现明细";
    }
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(125, 15, 20, 15)];
    [titleView addSubview:imageView];
    imageView.image = [UIImage imageNamed:@"mingxifenlei"];
    self.imageView = imageView;
    
    self.navigationItem.titleView = titleView;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAcion:)];
    [titleView addGestureRecognizer:tap];
    
    [self loadData];
    
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_header = header;
    // Do any additional setup after loading the view.
}


- (void)loadData{
    
    [ByCoreToolAlert showLoadind:@"加载中" hasMasker:YES];
    ByNetModel *netModel = [[ByNetModel alloc] init];
    if (self.type == 0) {
        netModel.type = 1;
    }else if (self.type == 1){
        netModel.type = 3;
    }else if (self.type == 2){
        netModel.type = 4;
    }
    [ByMoneyApi moneyDetailed:netModel success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert hideHud];
        [self.tableView.mj_header endRefreshing];
        [self.dataArray removeAllObjects];
        
        if ([result.data[@"data"] isKindOfClass:[NSArray class]]) {
            for (NSDictionary *dic in result.data[@"data"]) {
                ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
                [self.dataArray addObject:netModel];
            }
        }
        
        [self.tableView reloadData];
        SLog(@"明细 %@", result.data);
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
        [self.tableView.mj_header endRefreshing];
    }];
    
}

- (void)tapAcion:(UITapGestureRecognizer *)tap{
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
         self.imageView.image = [UIImage imageNamed:@"mingxifenlei 拷贝"];
    } completion:^(BOOL finished){
        
    }];
   
    ZZBPopView *popView = [[ZZBPopView alloc] init];
    __weak typeof(self) weakSelf = self;
    popView.indexAtView = ^(NSInteger index){
        weakSelf.type = index;
        if (index == 0) {
            weakSelf.titleLabel.text = @"账户明细";
        }else if (index == 1){
            weakSelf.titleLabel.text = @"充值明细";
        }else if (index == 2){
            weakSelf.titleLabel.text = @"提现明细";
        }

        weakSelf.imageView.image = [UIImage imageNamed:@"mingxifenlei"];
        [weakSelf loadData];
    };
    popView.type = self.type;
    [popView show];
}

- (void)initViews{
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(0);
        }
       
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.type == 0 || self.type == 1) {
    
        static NSString *cellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            UILabel *line = [[UILabel alloc] init];
            [cell addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell).offset(0);
                make.right.equalTo(cell).offset(0);
                make.bottom.equalTo(cell).offset(0);
                make.height.mas_equalTo(@1);
            }];
            line.backgroundColor = BY_LINE_COLOR;
        }
        for (UIView *view in cell.contentView.subviews) {
            [view removeFromSuperview];
        }
    
    UIImageView *iconView = [[UIImageView alloc] init];
    [cell.contentView addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(10);
        make.centerY.equalTo(cell.contentView).offset(0);
        make.size.mas_equalTo(CGSizeMake(45, 45));
    }];
    iconView.image = [UIImage imageNamed:@"touzhu"];
    
    
    UILabel *moneyLabel = [[UILabel alloc] init];
    [cell.contentView addSubview:moneyLabel];
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(cell.contentView).offset(-10);
        make.centerY.equalTo(cell.contentView).offset(0);
        make.height.mas_equalTo(@20);
        make.width.mas_equalTo(@100);
    }];
    moneyLabel.text = @"100元";
    moneyLabel.font = FONT(12);
    moneyLabel.textColor = [UIColor blackColor];
    moneyLabel.textAlignment = NSTextAlignmentRight;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [cell.contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconView.mas_right).offset(10);
        make.top.equalTo(iconView.mas_top).offset(0);
        make.height.mas_equalTo(@20);
        make.right.equalTo(moneyLabel.mas_left).offset(-10);
    }];
    
    titleLabel.font = FONT(14);
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    
    UILabel *timeLabel = [[UILabel alloc] init];
    [cell.contentView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_left).offset(0);
        make.bottom.equalTo(iconView.mas_bottom).offset(0);
        make.height.mas_equalTo(@20);
        make.right.equalTo(titleLabel.mas_right).offset(0);
    }];
    timeLabel.text = @"";
    timeLabel.font = FONT(12);
    timeLabel.textColor = [UIColor lightGrayColor];
    timeLabel.textAlignment = NSTextAlignmentLeft;
    
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    if (self.type == 1) {
        
        iconView.image = [UIImage imageNamed:@"chongzhi-1"];
        titleLabel.text = @"充值";
        timeLabel.text = [NSDate getDateStringFromeNumber:[NSNumber numberWithInteger:netModel.add_time.integerValue]];
        moneyLabel.text = [NSString stringWithFormat:@"%@元",netModel.recharge_money];
        
    }else if (self.type == 0){
        
        moneyLabel.text = [NSString stringWithFormat:@"%@元",netModel.user_money];
        timeLabel.text = [NSDate getDateStringFromeNumber:[NSNumber numberWithInteger:netModel.add_time.integerValue]];
        if (netModel.moneyType.integerValue == 1) {//1.投注
            iconView.image = [UIImage imageNamed:@"touzhu"];
            titleLabel.text = @"投注";
            moneyLabel.text = [NSString stringWithFormat:@"%@元",netModel.user_money];
        }else if (netModel.moneyType.integerValue == 2){//奖金
            iconView.image = [UIImage imageNamed:@"jianjin奖金"];
            titleLabel.text = @"奖金";
        }else if (netModel.moneyType.integerValue == 3){//充值
            iconView.image = [UIImage imageNamed:@"chongzhi-1"];
            titleLabel.text = @"充值";
            moneyLabel.text = [NSString stringWithFormat:@"+%@元",netModel.recharge_money];
            
        }else if (netModel.moneyType.integerValue == 4){//提现
            iconView.image = [UIImage imageNamed:@"tixian-1提现"];
            titleLabel.text = @"提现中";
            moneyLabel.text = [NSString stringWithFormat:@"%@元",netModel.user_money];
        }else if (netModel.moneyType.integerValue == 5){//退款
            iconView.image = [UIImage imageNamed:@"账单明细退款"];
            titleLabel.text = @"退款";
        }else if (netModel.moneyType.integerValue == 6){//赠金
            iconView.image = [UIImage imageNamed:@"赠送金额可用"];
            titleLabel.text = @"赠金成功";
        }else if (netModel.moneyType.integerValue == 7){//提现成功
            
            iconView.image = [UIImage imageNamed:@"账单明细提现成功"];
            titleLabel.text = @"提现成功";
            moneyLabel.text = [NSString stringWithFormat:@"%@元",netModel.user_money];
            
        }
        else if (netModel.moneyType.integerValue == 8){
            
            iconView.image = [UIImage imageNamed:@"账单明细提现失败"];
            titleLabel.text = @"提现失败";
            
        }else if (netModel.moneyType.integerValue == 9){
            
            iconView.image = [UIImage imageNamed:@"账单明细推广佣金"];
            titleLabel.text = @"推广佣金";
            
        }else if (netModel.moneyType.integerValue == 10){//赠金
            iconView.image = [UIImage imageNamed:@"赠送金额退回"];
            titleLabel.text = @"赠金退回";
        }
        
    }else if (self.type == 2){
        
        iconView.image = [UIImage imageNamed:@"tixian-1提现"];
        titleLabel.text = @"提现";
        moneyLabel.text = [NSString stringWithFormat:@"%@元",netModel.user_money];
        timeLabel.text = [NSDate getDateStringFromeNumber:[NSNumber numberWithInteger:netModel.add_time.integerValue]];
       
        
    }
     return cell;
        
    }else{
        
        ByWithdrawListCell *cell = [ByWithdrawListCell cellWithTableView:tableView];
        ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
        cell.netModel = netModel;
        return cell;
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
